package com.cm367.teamc;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import com.cm367.teamc.bean.Holiday;
import com.cm367.teamc.bean.Rule;
import com.cm367.teamc.message.EMessage;
import com.cm367.teamc.service.LeaveRulesService;
import com.cm367.teamc.service.LeaveService;
import com.cm367.teamc.session.Language;
import com.cm367.teamc.session.UserSession;

@ManagedBean
public class CreateLeaveRules {

	@ManagedProperty(value = "#{eMessage}")
	private EMessage eMessage;

	@ManagedProperty(value = "#{leaveRulesService}")
	private LeaveRulesService leaveRulesService;

	@ManagedProperty(value = "#{userSession}")
	private UserSession userSession;

	@ManagedProperty(value = "#{leaveSerivce}")
	private LeaveService leaveService;

	@ManagedProperty(value = "#{language}")
	private Language language;

	public EMessage geteMessage() {
		return eMessage;
	}

	public void seteMessage(EMessage eMessage) {
		this.eMessage = eMessage;
	}

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	private String leaveType = "";
	private String name;
	private int days;
	private Date date;

	private String result = "";

	private List<Rule> rules;
	private List<Holiday> holidays;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

	public LeaveRulesService getLeaveRulesService() {
		return leaveRulesService;
	}

	public void setLeaveRulesService(LeaveRulesService leaveRulesService) {
		this.leaveRulesService = leaveRulesService;
	}

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public String getLeaveType() {
		return leaveType;
	}

	public void setLeaveType(String leaveType) {
		this.leaveType = leaveType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getResult() {
		return result;
	}

	public List<Rule> getRules() {
		rules = leaveRulesService.findAllRules();
		return rules;
	}

	public List<Holiday> getHolidays() {
		holidays = leaveRulesService.findAllHolidays();
		return holidays;
	}

	public String create() {
		if (leaveType.equals("") && date == null) {
			return null;
		}
		if (date != null) {
			if (leaveRulesService.addPublicHoliday(name, date)) {
				result = eMessage.holidaySuccess(date);
			} else {
				result = eMessage.holidayFail(date);
			}
		}
		if (!leaveType.equals("")) {
			if (leaveRulesService.addTypeDays(leaveType, days)) {
				result = eMessage.typeSuccess(leaveType, days);
			} else {
				result = eMessage.typeFail(leaveType);
			}
		}
		return null;
	}

	public String deleteType() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		String type = params.get("RTYPE");
		if (leaveRulesService.deleteTypeDays(type)) {
			return null;
		}
		return null;
	}

	public String deleteHoliday() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		String date = params.get("HDATE");
		if (leaveRulesService.deletePublicHoliday(date)) {
			return null;
		}
		return null;
	}
}
